package com.jinniulive.platform.module.news;


import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.GridView;
import android.widget.ImageView;

import com.bigkoo.convenientbanner.ConvenientBanner;
import com.bigkoo.convenientbanner.holder.CBViewHolderCreator;
import com.bigkoo.convenientbanner.holder.Holder;
import com.bigkoo.convenientbanner.listener.OnItemClickListener;
import com.bumptech.glide.Glide;
import com.google.gson.Gson;
import com.jcodecraeer.xrecyclerview.XRecyclerView;
import com.jinniulive.platform.R;
import com.jinniulive.platform.adapter.NewsGridViewAdapter;
import com.jinniulive.platform.adapter.NewsListAdapter;
import com.jinniulive.platform.bean.NewsListInfo;
import com.jinniulive.platform.tools.GetInfoUtil;
import com.jinniulive.platform.urls.NetConfig;

import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

import java.util.ArrayList;
import java.util.List;

/**
 * A simple {@link Fragment} subclass.
 */
public class NewsFragment extends Fragment {


    private int startIndex = 1;
    private int count = 10;
    private XRecyclerView xRecyclerView;
    private NewsListAdapter mAdapter;
    private int userId;
    private String tokenId;
    private boolean isFirstLoad = true;
    private List<NewsListInfo.NewsdataBean.SamplenewsBean> mData;
    private boolean isPullDown = false;
    private ConvenientBanner convenientBanner;
    private GridView gv_news;
    private NewsGridViewAdapter adapter;
    private List<NewsListInfo.NewsdataBean.NewsBean> news;

    public NewsFragment() {

    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_new, container, false);
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        xRecyclerView = (XRecyclerView) view.findViewById(R.id.news_xRecyclerView);

        userId = GetInfoUtil.getInstance().getUserId(getActivity());
        tokenId = GetInfoUtil.getInstance().getTokenId(getActivity());


        View newsHead = LayoutInflater.from(getContext()).inflate(R.layout.item_news_head, null);
        convenientBanner = (ConvenientBanner) newsHead.findViewById(R.id.news_ConvenientBanner);
        gv_news = (GridView) newsHead.findViewById(R.id.gv_news);
        xRecyclerView.addHeaderView(newsHead);

        LinearLayoutManager layoutManager = new LinearLayoutManager(getContext());
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        xRecyclerView.setLayoutManager(layoutManager);

        adapter = new NewsGridViewAdapter(getContext());
        mAdapter = new NewsListAdapter(getContext());
        download();
        xRecyclerView.setAdapter(mAdapter);

        xRecyclerView.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
                //刷新的时候
                isPullDown = true;
                startIndex = 1;
                count = 10;
                download();
            }

            @Override
            public void onLoadMore() {
                //加载更多
                isPullDown = false;
                startIndex = 1;
                count = 10;
                download();
            }
        });

        mAdapter.setOnItemClickListener(new NewsListAdapter.OnItemClickListener() {
            @Override
            public void itemClickListener(View view, int position) {
                // TODO: 2016/12/13 咨询新闻条目点击事件
            }
        });
    }

    private void download() {
        String path = String.format(NetConfig.NEWSLIST, userId, tokenId, startIndex, count);
        Log.i("newspath ", path);
        x.http().post(new RequestParams(path), new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                Gson gson = new Gson();
                NewsListInfo newsListInfo = gson.fromJson(result, NewsListInfo.class);
                NewsListInfo.NewsdataBean newsdata = newsListInfo.getNewsdata();
                List<NewsListInfo.NewsdataBean.SlideImageDataBean> slideImageData = newsdata.getSlideImageData();
                List<NewsListInfo.NewsdataBean.SamplenewsBean> samplenews = newsdata.getSamplenews();
                news = newsdata.getNews();
                List<String>  image_urls = new ArrayList<>();
                for (NewsListInfo.NewsdataBean.SlideImageDataBean e : slideImageData) {
                    image_urls.add(e.getImageUrl());
                }

                convenientBanner.setPages(new CBViewHolderCreator<LocalImageHolderView>() {
                    @Override
                    public LocalImageHolderView createHolder() {
                        return new LocalImageHolderView();
                    }
                }, image_urls)
                        .setPageIndicator(new int[]{R.drawable.ic_page_indicator, R.drawable.ic_page_indicator_focus})
                        //设置指示器的方向
                        .setPageIndicatorAlign(ConvenientBanner.PageIndicatorAlign.CENTER_HORIZONTAL);
                convenientBanner.setOnItemClickListener(new OnItemClickListener() {
                    @Override
                    public void onItemClick(int position) {
                        //顶部轮播图
                        // TODO: 2016/12/13 顶部轮播图点击事件
                    }
                });

                adapter.setData(news);
                gv_news.setAdapter(adapter);
                mData=samplenews;
                mAdapter.setData(samplenews);

            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {

            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {

            }
        });
    }

    private class LocalImageHolderView implements Holder<String> {
        private ImageView imageView;

        @Override
        public View createView(Context context) {
            imageView = new ImageView(context);
            imageView.setScaleType(ImageView.ScaleType.FIT_XY);
            return imageView;
        }

        @Override
        public void UpdateUI(Context context, int position, String data) {
            Log.i("image_url", data);
            Glide.with(context).load(data).into(imageView);
        }
    }
}
